package com.eflow.model.flowable.converter;

import cn.hutool.core.bean.BeanUtil;
import com.eflow.model.domain.EflowUser;
import com.eflow.model.flowable.entity.ScriptEntity;
import com.eflow.model.flowable.model.DeptElement;
import com.eflow.model.flowable.model.UserElement;
import com.eflow.model.flowable.model.e.UserTypeEnum;
import com.eflow.model.groovy.GroovyScriptEngine;
import com.eflow.model.service.IEflowUserService;
import org.flowable.engine.repository.ProcessDefinition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author hui se
 * @description
 * @create 2021-03-05 18:21
 **/
@Component
public class UserDeptConverter extends BaseUserListConverter  {

    @Autowired
    private IEflowUserService eflowUserService;


    @Override
    List<EflowUser> list(UserElement userElement, String loginUser, ProcessDefinition processDefinition, String processInstanceId, String activityId, Map<String,Object> var) {
       List<Map> data = (List)userElement.getData();
        List<String> deptIds = data.stream().map(x -> {
            DeptElement deptElement = new DeptElement();
            return BeanUtil.fillBeanWithMap(x, deptElement, true).getId();
        }).collect(Collectors.toList());
        return  eflowUserService.selectUserByDeptIds(deptIds);
    }

    @Override
    UserTypeEnum getUserType() {
        return UserTypeEnum.ORG_LIST;
    }
}
